package gov.pbc.nn.kjgl.software.view;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.uploader.UploadFile;
import com.bstek.dorado.uploader.annotation.FileResolver;

import gov.pbc.nn.kjgl.entity.Project;
import gov.pbc.nn.kjgl.entity.ProjectFile;
import gov.pbc.nn.kjgl.entity.ProjectMember;
import gov.pbc.nn.kjgl.entity.ProjectProblem;
import gov.pbc.nn.kjgl.software.dao.ProjectJdbcDao;
import gov.pbc.nn.kjgl.software.dao.ProjectProblemDao;

@Component
public class ProjectProblemPR {
	
	@Autowired
	private ProjectProblemDao projectProblemDao;

	@Autowired
	private ProjectJdbcDao projectJdbcDao;
	
	@DataProvider
	public void getProjectProblem(Page<ProjectProblem> page,Criteria criteria, String Id) {
		this.projectProblemDao.getProjectProblem(page,criteria,Id);
	}
	
//	@DataProvider
//	public void getProject(Page<Project> page,Criteria criteria) {
//		this.projectJdbcDao.getProject(page,criteria);
//	}
	
	@DataProvider
	public List<ProjectMember> getProjectMember(String projectId) {
		return projectProblemDao.getProjectMember(projectId);
	}
	
	@DataProvider
	public void getProjectFile(Page<ProjectFile> page,String id) {
		this.projectProblemDao.getProjectFile(page,id);
	}
	
	
	@Transactional
	@DataResolver
	public void saveProjectProblem(ProjectProblem projectProblem,String Id) throws IOException {
		projectProblemDao.saveProjectProblem(projectProblem,Id);
	}
	
	@Transactional
	@Expose
	public void deleteProject(List<String> list) {
		for (String id : list) {
			projectProblemDao.delete(id, ProjectProblem.class);
		}
	}
	@Transactional
	@DataResolver
	public void deleteProjectFile(List<ProjectFile> list) throws IOException {
		if(list!=null){
			for (ProjectFile pf : list) {
				projectProblemDao.delete(pf);
			}
			for (ProjectFile pf : list) {
				String file = pf.getPath();
				Files.deleteIfExists(Paths.get(file));
			}
		}
		
	}
	
	/**
	 * 上传附件
	 * 
	 * @param file
	 * @param parameter
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	@FileResolver
	@Transactional
	public Object process2(UploadFile file, Map<String, Object> parameter)
			throws Exception {
		String problemId=(String) parameter.get("problemId");
		//判断问题ID。如果为空，则为新增问题。否则就是修改问题。（修改不能对文件进行修改，所以ProjectFile是新增）
		if(StringUtils.isEmpty(problemId)){
			//问题数据尚未保存，需要将上传文件保存到临时目录中，以后当问题数据保存的时候将临时目录中的该文件转移到实际目录中，并更新文件地址
			Path tmpFile = Files.createTempFile(UUID.randomUUID().toString(), null);//文件名自动生成。后缀名为null。
			//文件，文件路径。如果已经存在，就替换原有的文件，然后返回文件路径
			Files.copy(file.getInputStream(), tmpFile,StandardCopyOption.REPLACE_EXISTING);
			return tmpFile.toString();
		}else{
			Path dest = this.projectProblemDao.getRealPath();//上传文件保存后的实际地址
			
			//在实际将文件保存到实际地址前要进行数据库操作，这是为了实现文件保存到磁盘时出现异常时能够使数据库事务回滚。
			ProjectFile pf = new ProjectFile();
			pf.setOwnerId(problemId);
			pf.setFileName(file.getFileName());
			pf.setPath(dest.toString());
			pf.setType(3);
			this.projectProblemDao.save(pf);
			
			Files.copy(file.getInputStream(), dest);//文件真实路径
			return pf;
		}
	}
}
